<?php

class ProfileController extends Controller
{
	public function actionIndex(){
		if(isset(Yii::app()->session['_LOGGED_USER'])){
			$model = Yii::app()->session['_LOGGED_USER'];
			$user = Users::model()->findByPk($model['id']);
			$this->render('index', compact('user'));
		}else{
			$this->redirect('/');
		}
	}

	public function actionUpdateinfo(){
		if(isset(Yii::app()->session['_LOGGED_USER']) &&
			isset($_POST['first_name'])){
			$user = Yii::app()->session['_LOGGED_USER'];
			$model = Users::model()->findByPk($user['id']);
			$model->first_name = $_POST['first_name'];
			$model->last_name = $_POST['last_name'];
			$model->address = $_POST['address'];
			$model->email = $_POST['email'];
			if($model->save()){
				echo CJSON::encode(1);
			}
		}
	}

	public function actionNotification(){
		if(isset(Yii::app()->session['_LOGGED_USER'])){
			$user = Yii::app()->session['_LOGGED_USER'];
			$criteria = new CDbCriteria;
			$criteria->condition = 'user_id=:user';
			$criteria->params = array(':user' => $user['id']);
			$criteria->order = 'created_date DESC';
			$data = Notification::model()->findAll($criteria);
			$this->render('notification', compact('data'));
		}
	}
}